import { action } from 'mobx';
import i18n from 'i18n-js';
import {
  loginByMobile,
  getMobileCode,
  loginByMail,
  getMailCode,
  registerByMail,
  loginByPwd,
  resetPwd,
} from '../services/aepApi';
import Toast from '../utils/Toast';
import { login } from '../utils/loginStatus';
import regular from '../utils/regular';
import userStateModel from './userState';

export interface ILoginState {
  login(mobile: string, code: string): any;
  getCode(mobile: string): any;
}

class LoginState implements ILoginState {

  @action
  async login(mobile: string, code: string) {
    let data: IResult;
    if (regular.email.test(mobile)) {
      // 邮箱登录
      data = await loginByMail({ mail: mobile, code });
    } else {
      // 手机登录
      data = await loginByMobile({ mobile, code });
    }
    if (data.success) {
      await this.loginSuccess(data.data);
    } else {
      // 提示失败
      switch (
      parseInt(`${data.code}`, 10) // eslint-disable-line
      ) {
        case 1000034: {
          const AUTH_CODE_ERROR = i18n.t('AUTH_CODE_ERROR');
          Toast.error(AUTH_CODE_ERROR);
          break;
        }
        case 1000035:
          Toast.error(i18n.t('CODE_EXPIRE'));
          break;
        case 504:
          Toast.error(i18n.t('ACCOUNT_ERROR'));
          break;
        default: {
          const LOGIN_FAILED_PLEASE_TRY_AGAIN = i18n.t(
            'LOGIN_FAILED_PLEASE_TRY_AGAIN'
          );
          Toast.error(LOGIN_FAILED_PLEASE_TRY_AGAIN);
        }
      }
    }
    return data;
  }

  @action
  async loginByPassword(account: string, password: string) {
    let data: IResult;
    data = await loginByPwd({ account, password });
    if (data.success) {
      // this.loginSuccessAndGoToMain(data.data);
    } else {
      // 提示失败
      switch (
      parseInt(`${data.code}`, 10) // eslint-disable-line
      ) {
        case 1000000: {
          Toast.error(i18n.t('USER_NOT_EXIST'));
          break;
        }
        case 1000033: {
          Toast.error(i18n.t('ACCOUNT_OR_PASS_ERROR'));
          break;
        }
        default: {
          const LOGIN_FAILED_PLEASE_TRY_AGAIN = i18n.t(
            'LOGIN_FAILED_PLEASE_TRY_AGAIN'
          );
          Toast.error(LOGIN_FAILED_PLEASE_TRY_AGAIN);
        }
      }
    }
    return data;
  }

  @action
  async register(account: string, password: string, code: string) {
    let data: IResult;
    data = await registerByMail({ account, password, code });
    if (data.success) {
      Toast.success(i18n.t('REGSITER_SUCCESS_TO_LOGIN'));
    } else {
      switch (parseInt(`${data.code}`, 10)) {
        case 1000030:
          Toast.error(i18n.t('MAILBOX_ALREADY_TIED'));
          break;
        case 1000034:
          Toast.error(i18n.t('AUTH_CODE_ERROR'));
          break;
        case 1000035:
          Toast.error(i18n.t('CODE_EXPIRE'));
          break;
        default:
          Toast.error(i18n.t('REGSITER_ERROR_AGAIN'));
      }
    }
    return data;
  }

  @action
  async resetPassword(email: string, password: string, code: string) {
    let data: IResult;
    data = await resetPwd({ email, password, code });
    if (data.success) {
      Toast.success(i18n.t('RESET_SUCCESS_TO_LOGIN'));
    } else {
      switch (parseInt(`${data.code}`, 10)) {
        case 1000035:
          Toast.error(i18n.t('CODE_EXPIRE'));
          break;
        case 1000067:
          Toast.error(i18n.t('EMAIL_USER_NOT_EXIST'));
          break;
        default:
          Toast.error(i18n.t('RESET_PASSWORD_ERROR'));
      }
    }
    return data;
  }

  @action
  async getCode(mobile: string) {
    let data: IResult;
    if (regular.email.test(mobile)) {
      // 获取邮箱验证码
      data = await getMailCode({ mail: mobile });
    } else {
      data = await getMobileCode({ mobile });
    }
    if (data.success) {
      const INFO = i18n.t('AUTH_CODE_IS_SENT_SUCCESSFULLY');
      Toast.success(INFO);
    } else if (data.message) {
      if (data.message === 'yunpian send sms response error: code = 8') {
        // 提示不可频繁发送验证码
        const INFO = i18n.t('PLEASE_DO_NOT_FREQUENTLY_GET_VERFICATION_CODE');
        Toast.error(INFO);
      } else {
        switch (parseInt(`${data.code}`, 10)) {
          case 504:
            Toast.error(i18n.t('ACCOUNT_STYLE_ERROR'));
            break;

          default:
            const INFO_AUTH = i18n.t('AUTH_CODE_IS_SENT_ERROR');
            Toast.error(INFO_AUTH);
        }
      }
    }
    // else {
    //   // 网络错误
    //   const UNKNOW_ERROR = i18n.t('MAKE_SURE_THE_NETWORK_IS_OPEN');
    //   Toast.error(UNKNOW_ERROR, 3);
    // }
    return data;
  }


  async loginSuccess(data: any) {
    const {
      uid,
      jwtAuthenticationDto: { token },
      refreshToken,
      expiredAt,
      createdAt,
    } = data;
    await login({
      token,
      uid,
      refreshToken,
      expiredAt,
      createdAt,
    });
    await userStateModel.getUserInfo();
  }

}

export default new LoginState();
